package com.bio.tests;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SerialUtil {

    public static String Save(Object obj, String filename) {
        try {
            //use buffering
            OutputStream file = new FileOutputStream(filename);
            OutputStream buffer = new BufferedOutputStream(file);
            ObjectOutput output = new ObjectOutputStream(buffer);
            try {
                output.writeObject(obj);
            } finally {
                output.close();
            }
            return filename;
        } catch (IOException ex) {
            fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
        }
        return null;
    }

    public static Object load(String filename) {
        Object result = null;
        try {
            //use buffering
            InputStream file = new FileInputStream(filename);
            InputStream buffer = new BufferedInputStream(file);
            ObjectInput input = new ObjectInputStream(buffer);
            try {
                result = input.readObject();
            } finally {
                input.close();
            }
        } catch (ClassNotFoundException ex) {
            fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
        } catch (IOException ex) {
            fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
        }
        
        return result;
    }
    // PRIVATE //
    //Use Java's logging facilities to record exceptions.
    //The behavior of the logger can be configured through a
    //text file, or programmatically through the logging API.
    private static final Logger fLogger =
            Logger.getLogger(SerialUtil.class.getPackage().getName());
}
